<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      table {
        width: 500px;
        text-align: center;
        border-collapse: collapse;
      }
      th,
      td {
        border: 1px solid purple;
      }
    </style>
  </head>

  <body>
    <input type="text" placeholder="请输入订单状态" class="orderStatus" />
    <button>筛选订单</button><br /><br />
    <table>
      <thead>
        <tr>
          <th>订单编号</th>
          <th>姓名</th>
          <th>地址</th>
          <th>状态</th>
          <th>其他</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>

    <script>
      const orders = [
        {
          id: 1,
          name: 'John Smith',
          address: '123 Main St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-02-16 12:30:00',
            },
          ],
        },
        {
          id: 2,
          name: 'Jane Doe',
          address: '456 Second St',
          status: 'packed',
          log: [
            {
              status: 'packed',
              time: '2023-02-16 12:30:00',
            },
            {
              status: 'created',
              time: '2023-02-16 10:00:00',
            },
          ],
        },
        {
          id: 3,
          name: 'Bob Johnson',
          address: '789 Third St',
          status: 'shipped',
          log: [
            {
              status: 'created',
              time: '2023-02-15 10:00:00',
            },
            {
              status: 'packed',
              time: '2023-02-15 12:00:00',
            },
            {
              status: 'shipped',
              time: '2023-02-16 9:00:00',
            },
          ],
        },
        {
          id: 4,
          name: 'zs',
          address: '789 Third St',
          status: 'created',
          log: [
            {
              status: 'created',
              time: '2023-04-15 10:00:00',
            },
          ],
        },
      ]

      const btn = document.querySelector('button')
      const tbody = document.querySelector('tbody')

      btn.addEventListener('click', () => {
        const value = document.querySelector('.orderStatus').value
        if (!value) return alert('请输入正确的订单状态！')

        const res = orders.filter((item) => {
          return item.status === value
        })

        render(res)
      })

      tbody.addEventListener('click', (e) => {
        const id = +e.target.dataset.id
        const res = orders.find((item) => item.id === id).log

        const sortLog = res.sort((a, b) => {
          const timeA = +new Date(a.time)
          const timeB = +new Date(b.time)
          return timeA - timeB
        })

        alert(`
          物流信息：
          ${sortLog
            .map((item) => {
              return `
              [${item.time}] - [${item.status}]
            `
            })
            .join('')}
        `)
      })

      render()

      function render(data = orders) {
        const htmlStr = data.map((item) => {
          return `
              <tr>
                <td>${item.id}</td>
                <td>${item.name}</td>
                <td>${item.address}</td>
                <td>${item.status}</td>
                <td><a href="javascript:;" data-id=${item.id}>查看物流详情</a></td>
              </tr>`
        })
        tbody.innerHTML = htmlStr.join('')
      }
    </script>
  </body>
</html>
